package ds.recursive;

import ds.sort.Sort;
import ds.util.GeneralApp;

public class MergeApp {

	public static void main(String args[])
	{
		int[] a = Sort.insertSort(GeneralApp.randomArray(3, 1000));
		int[] b = Sort.insertSort(GeneralApp.randomArray(5, 1000));
		int[] c = merge(a,b);

		GeneralApp.printArray(a);
		GeneralApp.printArray(b);
		GeneralApp.printArray(c);
		
	}
	
	public static int[] merge(int[] a, int[] b)
	{
		int[] r = new int[a.length + b.length];
		int aIndex=0,bIndex=0,rIndex=0;
		while(aIndex<a.length && bIndex<b.length)
		{
			if(a[aIndex]<b[bIndex])
				r[rIndex++] = a[aIndex++];
			else
				r[rIndex++] = b[bIndex++];
		}
		while(aIndex<a.length)
		{
			r[rIndex++] = a[aIndex++];
		}
		while(bIndex<b.length)
		{
			r[rIndex++] = b[bIndex++];
		}
		return r;
	}
}